package com.qs.service;

import cn.dev33.satoken.stp.StpInterface;
import com.qs.mapper.PermissionsMapper;
import com.qs.mapper.RoleMapper;
import com.qs.mapper.entity.Permissions;
import com.qs.mapper.entity.Role;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义权限加载接口实现类
 */
@Service// 保证此类被 SpringBoot 扫描，完成 Sa-Token 的自定义权限验证扩展
@RequiredArgsConstructor
public class StpInterfaceImpl implements StpInterface {
    private final RoleMapper roleMapper;
    private final PermissionsMapper permissionsMapper;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<Permissions> permissionsList = permissionsMapper.getPermissionListByUsername(loginId);
        return permissionsList.stream().map(Permissions::getName).toList();
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<Role> roleList = roleMapper.getRoleListByUsername(loginId);
        return roleList.stream().map(Role::getName).collect(Collectors.toList());
    }

}
